package api

import (
	"github.com/labstack/echo/v4"
	"longmen/server/pkg/common/api_models"
	"longmen/server/pkg/common/helper/const"
	string2 "longmen/server/pkg/common/helper/string"
	"longmen/server/pkg/common/helper/util"
)

// @Summary 获取公告列表
// @Description 获取公告列表
// @Accept  json
// @Produce  json
// @Tags Announcement
// @Param   sendBody body api_models.GetAnnouncementListReq true "body"
// @Success 0 {object} api_models.GetAnnouncementListResp "result"
// @Router /api/announcement/getAnnouncementList [POST]
func (base *Controller) GetAnnouncementList(c echo.Context) error {
	req := &api_models.GetAnnouncementListReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	page := string2.GetDefaultNum(req.Page, 1)
	size := string2.GetDefaultNum(req.Size, 20)
	resp, err := base.Service.GetAnnouncementList(page, size)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}

// @Summary 获取最新公告标题
// @Description 获取最新公告标题
// @Accept  json
// @Produce  json
// @Tags Announcement
// @Success 0 {object} api_models.GetLastAnnouncementResp "result"
// @Router /api/announcement/getLastAnnouncement [POST]
func (base *Controller) GetLastAnnouncement(c echo.Context) error {
	list, err := base.Service.GetLastAnnouncement()
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, list, "")
}

// @Summary 获取公告
// @Description 获取公告
// @Accept  json
// @Produce  json
// @Tags Announcement
// @Param   sendBody body api_models.GetAnnouncementReq true "body"
// @Success 0 {object} api_models.GetAnnouncementResp "result"
// @Router /api/announcement/getAnnouncement [POST]
func (base *Controller) GetAnnouncement(c echo.Context) error {
	req := &api_models.GetAnnouncementReq{}
	if err := util.Json2Struct(c.Get("body").(string), req); err != nil {
		return util.BadParam(c, err)
	}
	if err := _const.Validate.Struct(req); err != nil {
		return util.BadParam(c, err)
	}
	resp, err := base.Service.GetAnnouncement(req.Id)
	if err != nil {
		return util.BuildFail(c, 1, "", err)
	}
	return util.BuildSuccess(c, resp, "")
}
